#include "PrecompiledHeaders.h"
#include "QuickPlayManager.h"
#include "MenuLevelSelect.h"

/*
 * Grab state pointers
 */
void QuickPlayManager::getStatePointers()
{
    PlayManager::getStatePointers();
    levelSelect = (MenuLevelSelect *)findByName("level_select");
}

/*
 * Load the quick play level select screen
 */
void QuickPlayManager::enter(void)
{
    // display the level select screen
    pushGameState(findByName("level_select"));
    currentState = psSELECTED;
    
    // initialise the current session
    GameSession *session = new GameSession();
    sessions.push_back(session);
}

/*
 * Calls the enter function to redraw the GUI after being popped back to
 */
void QuickPlayManager::resume(void)
{
    updateStateFromMenu();
    updateStateFromSelect();
    switch(currentState)
    {
        case psPLAYING:
            // Return from play and update session
            PlayState::getMainPlayersSession();
            updateStateFromSession(sessions[0]);
            resume();
            return;
            
        case psSELECTED:
            // Load the level, display loading screen
            menu->setLayout((LayoutType)LOAD);
            pushGameState(findByName("in_game_menu"));
            currentState = psLOADING;
            return;
            
        case psLOADING:
            // Set sessions
            sessions[0]->beginQuickPlay();
            suPlayState->setSession(sessions[0]);
            
            // Push game state 
            pushGameState(findByName("single_user_play"));
            currentState = psPLAYING;
            
            // Sort audio
            resources->audio->stopMenuBGM();
            resources->audio->playBGM();
            return;
            
        case psFINISHED:
            // Unload level and display level select
            resources->current_level->unload();
            menu->setState(lsNORMAL);
            pushGameState(findByName("level_select")); 
            currentState = psSELECTED;
            return;
            
        case psSUCCESSFUL:
            // Set session
            sessions[0]->endQuickPlay();
            
            // Load success page
            menu->setLayout((LayoutType)QP_LEVEL_COMPLETE);
            pushGameState(findByName("in_game_menu"));
            currentState = psFINISHED;
            
            // Set audio
            resources->audio->stopBGM();
            resources->audio->playMenuBGM();
            return;
            
        case psFAILED:
            // Set session
            sessions[0]->endQuickPlay();
            
            // Load fail page
            menu->setLayout((LayoutType)QP_FAILED);
            pushGameState(findByName("in_game_menu"));
            currentState = psFINISHED;
            
            // Set audio
            resources->audio->stopBGM();
            resources->audio->playMenuBGM();
            return;
        
        case psREPLAY_LEVEL:
            // Set sessions
            sessions[0]->beginQuickPlay();
            suPlayState->setSession(sessions[0]);
            currentState = psPLAYING;
            
            // Push on play state
            pushGameState(findByName("single_user_play"));
            
            // Set audio
            resources->audio->stopMenuBGM();
            resources->audio->playBGM();
            return;
            
        case psQUIT:
            clearSessions();
            popGameState();
            return;
            
        default:
            break;
    }
}

/*
 * This is a bit of a hack, but inspect menu select state to see if it quit
 */
void QuickPlayManager::updateStateFromSelect()
{
    switch(levelSelect->getState())
    {
        case mlsQUIT:
            currentState = psQUIT;
            levelSelect->setState(mlsNORMAL);
            return;
        default:
            break;
    }
}
